package com.decent.ejfadmin.equity.price.controller;

import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.equity.common.CardPriceInfo;
import com.decent.ejfadmin.equity.common.CardProduct;
import com.decent.ejfadmin.equity.price.service.SetCardPriceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * describe:权益产品密价设置
 *
 * @author zhangbin
 * @date 2019/03/19
 */
@RequestMapping("/cardPrice/")
@RestController
@Slf4j
public class SetCardPriceController {
    @Autowired
    private SetCardPriceService setCardPriceService;

    /**
     * 查询产品列表
     *
     * @param productType 运营商/卡种
     * @return
     */
    @RequestMapping(value = "queryProductList")
    public List<CardProduct> queryProductList(Integer productType) {
        try {
            return setCardPriceService.queryProductList(productType);
        } catch (Exception e) {
            log.warn("设置权益密价查询产品异常[{}][{}]", productType, e.getLocalizedMessage(), e);
            return Collections.emptyList();
        }
    }

    /**
     * 设置权益密价
     *
     * @param cardPriceInfo
     * @return MessageBean
     */
    @RequestMapping("setUpCardPrice")
    public MessageBean setUpCardPrice(CardPriceInfo cardPriceInfo) {
        try {
            return setCardPriceService.setUpCardPrice(cardPriceInfo);
        } catch (Exception e) {
            log.warn("设置权益密价异常[{}][{}]", cardPriceInfo, e.getLocalizedMessage(), e);
            String duplicate = "Duplicate entry";
            if (e.getLocalizedMessage().contains(duplicate)) {
                return MessageBean.fail("已存在相同的密价设置,请排除后再进行操作");
            }
            return MessageBean.fail("系统异常,操作失败");
        }
    }
}
